/*
 * Chandni Sabhaya
 * Date: 11/20/2011
 * User Object
 */


package caobject;

import java.io.Serializable;
import java.util.HashSet; // ctrl + shif + O = organize the import
import java.util.Set;

/**
 * This User class contains the name of the user, the user 's current role and 
 * the set of all roles.
 *   
 * @author Chandni Sabhaya
 * @Version Autumn 2011
 *
 */
public class User implements Serializable, Cloneable 
{
	/**
	 * Generated SV-UID
	 */
	private static final long serialVersionUID = 6408762417367674894L;

	/**
	 *  user name  
	 */
	private String my_user_name;
	
	/**
	 * It shows the user's current role. 
	 */
	private EnumRole my_current_role;
	
	/**
	 * set of roles. 
	 */
	private Set<EnumRole> my_roles; 
	
	/**
	 * Public Constructor. 
	 * 
	 * PreConditon :user name != null && User name length <= 10, && 
	 * 				user name should be trimmed.
	 * 
	 * @param the_user_name is the name of the user.
	 * @param the_role specifies the current role. 
	 */
	public User(final String the_user_name, final EnumRole the_role)
	{
		my_roles = new HashSet<EnumRole>();		
		my_user_name = the_user_name;		
		my_roles.add(the_role);		
		my_current_role = the_role;	 
	}
	
		
	/**
	 * Gets the name of this user.
	 * 
	 * @return the name of this user. 
	 */
	public String getUserName()
	{
		return my_user_name;
	}
	
	/**
	 * Gets the Role of this user. The Role could be author, reviewer or 
	 * Programmer-chair as listed in the enumeration. 
	 * 
	 * @return the name of this user. 
	 */
	public EnumRole getcurrentRole()
	{
		return my_current_role;
	}
	
	
	/**
	 * This get all the of the roles that user have. User could have one or more 
	 * than one role.
	 * 
	 * @return 
	 */
	public Set<EnumRole> getRoles()
	{
		return my_roles;
	}
	
	/**
	 * Sets the user's current role. 
	 * 
	 * Precondition: the_current_role != null
	 * @param the_current_role defines this user's current role.
	 */	
	public void setCurrentRole(final EnumRole the_current_role)
	{		
		my_current_role = the_current_role;
	}
	
	/**
	 * This method adds the new role to the set of roles. Since the user can 
	 * have more than one role.
	 *  
	 * Precondition: the_new_role != null
	 * 
	 * @param the_new_role which needs to be added to the set of roles. 
	 */
	public void addNewRole(final EnumRole the_new_role)
	{		
		my_roles.add(the_new_role);
	}
	
	/**
	 * User hashCode() method.
	 */
	@Override
	public int hashCode() 
	{
		return toString().hashCode();
	}
	
	/**
	 * User toString() method.
	 */
	@Override
	public String toString() 
	{
		final StringBuilder sb = new StringBuilder();
		sb.append("User Name : " + my_user_name+ "\n");
		sb.append("Current Role: " + my_current_role + "\n");
		sb.append("List of Roles: " + my_roles + "\n");
		return sb.toString();
	}
	
	public Object clone() 
	{
	    User user;
	    try 
	    {
	      user = (User) super.clone();
	    }
	    catch (CloneNotSupportedException e)
	    {
	      return null; // will never happen
	    }
	    return user;
	}
}
